$(function() {
	"use strict";
	var initOrgUserTree = function() {

		$("#org_user_tree").jstree({
			"core" : {
				"themes" : {
					"responsive" : false
				},
				"check_callback" : true,
				'data' : {
					'url' : function(node) {
						return context + '/console/org/tree/data/user1';
					},
					'data' : function(node) {
						return {
							'parent' : node.id
						};
					}
				}
			},
			"types" : {
				"default" : {
					"icon" : "fa fa-folder icon-state-warning icon-lg"
				},
				"file" : {
					"icon" : "fa fa-file icon-state-warning icon-lg"
				}
			},
			"plugins" : [ "contextmenu", "types" ],
			"contextmenu" : {
				"items" : {
					"可访问" : {
						"label" : "可访问",
						"action" : function(data) {
							var inst = jQuery.jstree.reference(data.reference),  
		                    obj = inst.get_node(data.reference);
							
							var pobjType = obj.id.substring(0,1);
							var pobjId = obj.id.substring(1,obj.id.length);
							var pType="Y";
							
							initMenuTreeForSelect(pobjType,pobjId,pType,obj.text);
							
						}
					},
					"不可访问":{
						"label":"不可访问",
						"action" : function(data) {
							var inst = jQuery.jstree.reference(data.reference),  
		                    obj = inst.get_node(data.reference);

							var pobjType = obj.id.substring(0,1);
							var pobjId = obj.id.substring(1,obj.id.length);
							var pType="N";
							
							initMenuTreeForSelect(pobjType,pobjId,pType,obj.text);
							
						}
					}
				}
			}
		});

	}
	
	var initMenuTreeForSelect = function(pobjType,pobjId,pType,text){
		var pobjTypeText='';
		if(pobjType=='G')
			pobjTypeText='机构';
		else if(pobjType=='U')
			pobjTypeText='用户';
		else if(pobjType=='R')
			pobjTypeText='角色';
		
		var pTypeText='';
		if(pType=='Y'){
			pTypeText='可访问';
		}else if(pType=='N'){
			pTypeText='不可访问';
		}
		$("input[name='pobjType']").val(pobjType);
		$("input[name='pobjId']").val(pobjId);
		$("input[name='pType']").val(pType);
		
		$("#authDesc").html("【"+pobjTypeText+"-"+text+"-"+pTypeText+"】");
		
		$("#menu_tree").jstree().deselect_all();
		
		$.ajax({
            url:context+'/console/auth/load',
            type:'POST',
            data:{pobjType:pobjType,pobjId:pobjId,pType:pType},
            dataType:'json',
            success:function(data,textStatus,jqXHR){
                if(data.success){
                	var menuIds = data.menuIds.split(",");
                	for(var i=0;i<menuIds.length;i++){
                		$("#menu_tree").jstree().select_node(menuIds[i]);
                	}
                }else{
                	toastr["error"]("加载授权信息异常", "提示");
                }
                
            }
        });
		
		
		$(".saveAuth").removeClass('disabled');
		$(".saveAuth").unbind('click').on('click',function(){
			var pobjType = $("input[name='pobjType']").val();
			var pobjId = $("input[name='pobjId']").val();
			var pType= $("input[name='pType']").val();
			
			var menuIds = $("#menu_tree").jstree().get_selected().join(",");
			
			
			$.ajax({
	            url:context+'/console/auth/save',
	            type:'POST',
	            data:{pobjType:pobjType,pobjId:pobjId,pType:pType,menuIds:menuIds},
	            dataType:'json',
	            success:function(data,textStatus,jqXHR){
	                if(data.success){
	                	toastr["info"]("保住授权信息成功", "提示");
	                }else{
	                	toastr["error"]("保存授权信息异常", "提示");
	                }
	                
	            }
	        });
			
		});
		
	}
	
	
	var initMenuTree = function(){


		$("#menu_tree").jstree({
			"core" : {
				"themes" : {
					"responsive" : false
				},
				"check_callback" : true,
				'data' : {
					'url' : function(node) {
						return context + '/console/menu/tree/data';
					},
					'data' : function(node) {
						return {
							'parent' : node.id
						};
					}
				}
			},
			"types" : {
				"default" : {
					"icon" : "fa fa-folder icon-state-warning icon-lg"
				},
				"file" : {
					"icon" : "fa fa-file icon-state-warning icon-lg"
				}
			},
			"plugins" : [ "checkbox", "types" ],
			"checkbox" : {
			      "three_state" : false
			    }
		});

	}
	
	var initRoleTable = function () {

        var table = $('#role_table');

        table.DataTable({
        	"searching": false,
            "language": {
                "emptyTable": "暂无数据",
                "loadingRecords": "请稍候 - 数据加载中...",
                "info": "显示 _START_ 到 _END_ 共 _TOTAL_ 条记录",
                "infoEmpty": "",
                "infoFiltered": "",
                "lengthMenu": "_MENU_",
                "zeroRecords": "暂无数据",
                "paginate": {
                    "previous":"上一页",
                    "next": "下一页",
                    "last": "最后一页",
                    "first": "第一页"
                }
            },
            "ordering": false,
            "lengthChange":false,
            "pageLength": 20,
            "pagingType": "simple_numbers",
            "serverSide": true,
            "ajax": {
                url: context+"/console/role/list",
                type: 'POST',
                dataSrc: 'roleList'
            },
            "columns": [
                { "data": "roleKey" },
                { "data": "roleName" },
                { "data": "roleId",
                  "width":200,
                  "render": function ( data, type, full, meta ) {
                    return '<a href="javascript:;" class="btn btn-default btn-sm op" data-op-id='+data+' data-op-type="Y" data-op-name='+full.roleName+' title="可访问"><i class="fa fa-eye"></i>可访问 </a>&nbsp;'+
                    	   '<a href="javascript:;" class="btn btn-danger btn-sm op" data-op-id="'+data+'" data-op-type="N" data-op-name='+full.roleName+' title="不可访问"><i class="fa fa-eye-slash"></i>不可访问 </a>&nbsp;'
                  }
                }
            ]
        });
        
        table.delegate(".op","click",function(){
        	var pobjType = "R";
			var pobjId = $(this).data('opId');
			var pType=$(this).data('opType');
			var text = $(this).data('opName');
			
			initMenuTreeForSelect(pobjType,pobjId,pType,text);
        });
        
    }

	initOrgUserTree();
	initMenuTree();
	initRoleTable();
});